package com.naodong.shenluntiku.intergation.aliyunoss;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by hujie on 16/6/6.
 *
 * 如果是语音就是
 mp3/2016/06/06/时间戳+随机字符串.mp3
 如果是图片就是
 pic/2016/06/06/时间戳+随机字符串.jpg/png/gif
 */
public class OssObject {




    public static String createImageOssObjectKey(String keyName) {
        String dateFormat = new SimpleDateFormat("yyyy/MM/dd/HH").format(new Date());
        StringBuilder sb = new StringBuilder("pic/");
        sb.append(dateFormat)
                .append("/")
                .append(keyName);

        return sb.toString();
    }


    public static String createImageOssObjectKeyByFile(String fileName) {

        String type = ".jpg";
        if (!TextUtils.isEmpty(fileName)) {
            int point = fileName.lastIndexOf('.');
            type = fileName.substring(point + 1);
        }
        return createImageOssObjectKey(createImageOssObjectKeyName(type));
    }

    /**
     * 创建图片的ossobject keyname
     * @return
     */
    private static String createImageOssObjectKeyName(String type) {
        long currentTimeMillis = System.currentTimeMillis();
        UUID uuid = UUID.randomUUID();
        String uniqueId = uuid.toString();
        return new StringBuffer().append(currentTimeMillis).append("-").append(uniqueId).append(".").append(type).toString();
    }
}
